package com.googlecode.jawb.tests;

import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;

import java.io.ByteArrayInputStream;

import org.junit.Before;
import org.junit.Test;

import com.googlecode.jawb.BinaryType;
import com.googlecode.jawb.JAWB;
import com.googlecode.jawb.clues.SetArray;
import com.googlecode.jawb.clues.SetCondition;
import com.googlecode.jawb.clues.SetType;

public class Loading1 {
	@SetType(type = BinaryType.UINT_8)
	private short value1;
	
	@SetType(type = BinaryType.UINT_16)
	private int value2;
	
	@SetType(type = BinaryType.UINT_32)
	private long value3;

	@SetType(type = BinaryType.UINT_8)
	@SetArray(staticLength = 3)
	private short[] staticArray;
	
	@SetType(type = BinaryType.UINT_8)
	private short length;
	
	@SetType(type = BinaryType.UINT_8)
	@SetArray(dynamicLength = "length")
	private short[] dynamicArray;
	
	@SetType(type = BinaryType.UINT_8)
	@SetCondition(methodName = "getTrue")
	private int cond1;
	
	private int hell = 666;
	
	private Loading1 loadedInstance;

	@SuppressWarnings("unused")
	private boolean getTrue() {
		return true;
	}
	
	@SuppressWarnings("unused")
	private boolean getFalse() {
		return false;
	}
	
	@Before()
	public void init() throws Exception {
		loadedInstance = (Loading1) new JAWB().loadFrom(new ByteArrayInputStream(new byte[]{
			(byte) 0xAA,											// value1
			(byte) 0xAA, (byte) 0xBB,								// value2
			(byte) 0xAA, (byte) 0xBB, (byte) 0xCC, (byte) 0xDD,		// value3
			(byte) 0x01, (byte) 0x02, (byte) 0x03,					// staticArray
			(byte) 0x04,											// length
			(byte) 0x0A, (byte) 0x0B, (byte) 0x0C, (byte) 0x0D,		// dynamicArray,
			(byte) 0xFF,											// cond1,
		}), Loading1.class);
	}

	@Test()
	public void instance() {
		assertNotNull(loadedInstance);
	}
	
	@Test()
	public void uintValues() {
		assertEquals(loadedInstance.value1, 0xAA);
		assertEquals(loadedInstance.value2, 0xAABB);
		assertEquals(loadedInstance.value3, 0xAABBCCDD);
	}
	
	@Test()
	public void staticArray() {
		assertArrayEquals(loadedInstance.staticArray, new short[]{1, 2, 3});
	}
	
	@Test()
	public void dynamicArray() {
		assertArrayEquals(loadedInstance.dynamicArray, new short[]{0x0A, 0x0B, 0x0C, 0x0D});
	}
	
	@Test()
	public void ignoration() {
		assertEquals(loadedInstance.hell, 666);
	}
	
	@Test()
	public void complexConds() {
		assertEquals(loadedInstance.cond1, 0xFF);
	}
}
